package com.beasts.antpage.sh.service;

import com.beasts.antpage.sh.entity.Layout;
import com.beasts.antpage.sh.entity.RemindMessage;
import com.beasts.antpage.sh.repository.LayoutRepository;
import com.beasts.antpage.sh.repository.MessageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Locker
 * @date 20/04/2018 11:02 AM
 * @since 1.0
 */
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageRepository messageRepository;

    @Override
    public List<RemindMessage> findAll(String name) {
        return messageRepository.findAll();
    }

    @Override
    public String addMessage(HttpServletRequest req) {
        //消息队列的code
        String  EventCode = req.getParameter("EventCode");
        //用户唯一标识
        String  UserId = req.getParameter("UserId");
        //业务系统编码
        String  AppId = req.getParameter("AppId");
        //系统名称
        String  FunctionName = req.getParameter("FunctionName");
        //消息标题
        String  Title = req.getParameter("Title");
        //消息链接
        String  Url = req.getParameter("Url");
        //消息内容
        String  Content = req.getParameter("Content");
//        try {
//            if(StringUtils.isNotEmpty(FunctionName)){
//                FunctionName = URLDecoder.decode(FunctionName,"UTF-8");
//            }
//
//            if(StringUtils.isNotEmpty(Title)){
//                Title = URLDecoder.decode(Title,"UTF-8");
//            }
//            if(StringUtils.isNotEmpty(Content)){
//                Content = URLDecoder.decode(Content,"UTF-8");
//            }
//
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        ;
        //业务日期
        String  BusiDate = req.getParameter("BusiDate");
        RemindMessage message = new RemindMessage();
        try {
            if(StringUtils.isNotEmpty(EventCode)){
                message.setEventCode(EventCode);
            }else{
                Date currentTime = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
                String dateString = formatter.format(currentTime);
                int randomNum = (int)(Math.random()*10000);
                String randomStr = String.valueOf(randomNum);
                dateString += randomStr;
                EventCode = dateString;
                message.setEventCode(dateString);
            }

            message.setUserId(UserId);
            message.setAppId(AppId);
            message.setFunctionName(FunctionName);
            message.setBusiDate(BusiDate);
            message.setTitle(Title);
            message.setContent(Content);
            message.setStatus("0");//状态：未读
            message.setUrl(Url);
            messageRepository.save(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EventCode;
    }

    @Override
    public String updateMessage(HttpServletRequest req) {


        //消息队列的code
        String  EventCode = req.getParameter("EventCode");
        //用户唯一标识
        String  UserId = req.getParameter("UserId");
        //业务系统编码
        String  AppId = req.getParameter("AppId");

        try {
            messageRepository.updateMessage("1" ,EventCode,UserId,AppId);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public List<RemindMessage> findByStatus(String status,String userId) {
        return messageRepository.findByStatusAndUserIdOrderByBusiDateDesc(status,userId);
    }

    @Override
    public RemindMessage findByEventCode(String eventCode) {
        return messageRepository.findByEventCode(eventCode);
    }

}
